import { _decorator, Button, Label, Node, Sprite, SpriteAtlas } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames } from '../../Config/ResConfig';
import { GameType } from '../../Config/GameCfg';
import { MsgDataGame } from '../../Data/MsgDataGame';
const { ccclass, property } = _decorator;

export let Dir = {
    L: 1,
    R: 2
}

export let Stutue = {
    Default:0,  //默认
    Dui: 1,     //答对
    Cuo: 2,     //答错
}

@ccclass('Block')
export class Block extends UIComponent {
    spBg: Sprite;
    labDes: Label;
    sprL: Node;
    sprR: Node;
    atals: SpriteAtlas;

    //题目id
    private _tid: number;
    btn: any;
    private _gameType: number; //游戏类型
    set gameType(v){
        this._gameType = v;
    }
    public get tid(){
        return this._tid;
    }

    //答案的索引值
    private _cid: number;
    public get cid(){
        return this._cid;
    }


    costTime:number = 0;

    //是否点击了，防止重复点击
    isClick: boolean;

    protected onLoad(): void {
        this.InitUI();
        this.InitData();
    }

    start() {
       
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        this.InitUI2();
        this.AddButtonListener("bg",this,this.onBtnClick);
        this.btn = this.ViewComponent('bg',Button);
    }

    private InitUI2(){
        this.spBg = this.ViewComponent('bg',Sprite);
        this.labDes = this.ViewComponent('bg/lab', Label);
        this.sprL = this.ViewNode('spL');
        this.sprR = this.ViewNode('spR');

        this.atals = ResManager.inst.getAsset(AbNames.GamePlay,'Atals/GamePlay',SpriteAtlas) as SpriteAtlas;
    }

    private InitData(){
        // console.log('=======Block=InitData=======');
        this._cid = -1;
        this._tid = -1;
        this.isClick = true;

        this.setLActive(false);
        this.setRActive(false);
        this.setBg(Stutue.Default);
        this.btn.interactable  = true;
    }

    public resetItem(){
        this.InitData();
    }

    private registerEvent(){
        // director.on(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private unRegisterEvent(){
        // director.off(EventMsgKes.loading,this.onLoadingPro,this);
    }

    /**
     * 
     * @param type 0 是默认， 1是答对 2是答错
     */
    private setBg(type: number){
        let sf = null;
        if(type == Stutue.Default){
            sf = this.atals.getSpriteFrame('GamePlay_btn1');
        }else if(type == Stutue.Dui){
            sf = this.atals.getSpriteFrame('GamePlay_btn3');
        }else if(type == Stutue.Cuo){
            sf = this.atals.getSpriteFrame('GamePlay_btn2');
        }
        if(sf){
            this.spBg.spriteFrame = sf;
        }
    }

    private setLab(str: string){
        if(!this.labDes){
            this.InitUI2();
        }
        this.labDes.string = str;
    }

    private setLActive(active: boolean){
        if(!this.sprL){
            this.InitUI2();
        }
        this.sprL.active = active;
    }

    private setRActive(active: boolean){
        if(!this.sprR){
            this.InitUI2();
        }
        this.sprR.active = active;
    }

    /**
     * 
     * @param status 答对是true, 答错是false
     * @param dir 1 是左边 2 是右边
     */
    public setStatue(status: boolean,dir: number = Dir.L){
        if(!this.atals){
            this.InitUI2();
        }

        let sf = null;
        if(status){
            sf = this.atals.getSpriteFrame('GamePlay_gou');
        }else{
            sf = this.atals.getSpriteFrame('GamePlay_x');
        }
        if(sf){
            if(dir == Dir.L){
                this.setLActive(true);
                this.sprL.getComponent(Sprite).spriteFrame = sf;
            }else if(dir == Dir.R){
                this.setRActive(true);
                this.sprR.getComponent(Sprite).spriteFrame = sf;
            }
        }
    }

    //点击答题
    private onBtnClick(btn:Button){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;

        // let gameType = globalThis.gameMgr.zts_type
        console.log('=======onBtnClick============',this.tid,this._cid,this._gameType);
        if(this._gameType === GameType.Game_ZT){
            let time = (new Date().getTime() - globalThis.gameMgr.zts_answerStart)/1000; 
            let t1 = time.toFixed(2);
            let t2 = parseInt(t1);
            globalThis.gameMgr._GameZtMsg.sendUserAnswerReq(this.tid,this._cid,t2);
        }else if(this._gameType === GameType.Game_TZ){ //挑战赛答题
            globalThis.gameMgr._GameTzMsg.StartAnswerTzReq(this.tid, this._cid);
        }
    }

    //初始化
    public setData(idx: number,des: string,tid:number){
        // console.log('=======Block=setData=======',idx,tid,des);
        this._cid = idx;
        this._tid = tid;
        this.isClick = true;
        this.setLActive(false);
        this.setRActive(false);
        this.setBg(Stutue.Default);
        this.setLab(des);
    }

    public ongAnswerRes(isTrue: boolean){
        if(isTrue){
            this.setBg(Stutue.Dui);
        }else{
            this.setBg(Stutue.Cuo);
        }
    }

    /**
     * 设置主题赛
     * @param isDui 是否答对
     * @param isSelf 是否是自己
     */
    public setMatchGameData(isDui: boolean, isSelf: boolean){
        if(isDui){
            this.setBg(Stutue.Dui);
            if(isSelf){
                this.setStatue(true,Dir.L);
            }else{
                this.setStatue(true,Dir.R);
            }
        }else{
            this.setBg(Stutue.Cuo);
            if(isSelf){
                this.setStatue(false,Dir.L);
            }else{
                this.setStatue(false,Dir.R);
            }
        }
    }

    //所有玩家答错展示对的
    public setMatchGameData2(){
        this.setBg(Stutue.Dui);
    }

    public setBtnStatus(isClick: boolean){
        if(this.btn){
            this.InitUI2();
        }
        this.btn.interactable  = isClick;
    }
}